package com.sgcc.pda.web.receiver;


import android.content.Context;
import android.text.TextUtils;

import com.sgcc.pda.data.entity.BaseEntity;
import com.sgcc.pda.tools.LogPrintUtils;
import com.sgcc.pda.util.NetWorkTools;

import com.sgcc.pda.util.PlatConstant;

import org.json.JSONObject;

import java.lang.reflect.ParameterizedType;

/**
 * @创建者：suven on 2020/3/24 10:55
 * @描述： 广播接收者
 * @版本：1.0
 * @修改人：
 * @修改时间：
 * @修改说明：
 * @修改版本：
 */
public abstract class CallReceiver<N>  implements IWebCall {

    private Context mContext;
    String mTag;

    private Class<?> mDataClass;


    /**
     * 注册广播
     *
     * @param context
     */
    public CallReceiver(Context context) {
        this.mContext = context;
        this.mTag = NetWorkTools.
                getRandomBoradcastName(context);
        mDataClass = getParameterizedType();
        LogPrintUtils.d("mTag： " + mTag);

    }

    @Override
    public Context getContext() {
        return mContext;
    }

    @Override
    public String getTag() {
        return  this.mTag;
    }



    @Override
    public void onDownLoading(int progress) {

    }

    @Override
    public void onDownLoading(long current, long total) {

    }

    @Override
    public void onDownLoading(int current, int total, int status) {

    }

    @Override
    public void onSuccess(String result) {
        try {
            if (TextUtils.isEmpty(result)) {
                LogPrintUtils.d(String.format("接口信息返回为空 %s", PlatConstant.IWebState.none));
                onFail(PlatConstant.IWebState.none, "接口信息返回为空");
            } else {
                if (String.class.isAssignableFrom(mDataClass)) {
                    onResult(result);
                } else if (JSONObject.class.isAssignableFrom(mDataClass)) {
                    onResult(new JSONObject(result));
                } else if (BaseEntity.class.isAssignableFrom(mDataClass)) {
                    BaseEntity entity = BaseEntity.objectFromData(result, (Class<? extends BaseEntity>) mDataClass);
                    if (null != entity && !TextUtils.isEmpty(entity.RT_F)) {
                        if (PlatConstant.IWebState.success.equalsIgnoreCase(entity.RT_F)) {
                            LogPrintUtils.d(String.format("成功 %s", PlatConstant.IWebState.success));
                            onResult((N) entity);
                        } else {
                            LogPrintUtils.d(entity.RT_D + entity.RT_F);
                            onFail(entity.RT_F, entity.RT_D);
                        }
                    } else {
                        LogPrintUtils.d(String.format("接口返回解析错误 %s", PlatConstant.IWebState.encrypterror));
                        onFail(PlatConstant.IWebState.encrypterror, "接口返回解析错误");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogPrintUtils.d(String.format("接口返回产生异常 %s", PlatConstant.IWebState.error));
            LogPrintUtils.d(e);
            onFail(PlatConstant.IWebState.error, "接口返回产生异常");
        }
    }

    private Class<?> getParameterizedType() {
        try {
            ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
            return (Class<?>) genericSuperclass.getActualTypeArguments()[0];
        } catch (Exception e) {
            e.printStackTrace();
            LogPrintUtils.d("getParameterizedType:异常",e);
        }
        return BaseEntity.class;
    }


}
